<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
<head>
    <title></title>
    <link href="<s:url value='/common/lib/ligerUI/skins/Aqua/css/ligerui-all.css'/>" rel="stylesheet" type="text/css" />
    <link href="<s:url value='/common/lib/ligerUI/skins/ligerui-icons.css'/>" rel="stylesheet" type="text/css" />
    <script src="<s:url value='/common/lib/jquery/jquery-1.3.2.min.js'/>" type="text/javascript"></script>
    <script src="<s:url value='/common/lib/ligerUI/js/core/base.js'/>" type="text/javascript"></script>
    <script src="<s:url value='/common/lib/ligerUI/js/plugins/ligerTab.js'/>" type="text/javascript"></script>
    <script src="<s:url value='/common/lib/ligerUI/js/plugins/ligerTree.js'/>" type="text/javascript"></script>
    <script src="<s:url value='/common/lib/ligerUI/js/plugins/ligerDialog.js'/>" type="text/javascript"></script>
    <script src="<s:url value='/common/lib/ajaxfileupload.js'/>" type="text/javascript"></script>
    <script src="<s:url value='/common/dealer.js'/>" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#paramBox").hide();
            $("#tabs").hide();
            var tree = $("#tree1").ligerTree({
				single:true,
                nodeDraggable: false,
                checkbox:false,
                onSelect:function(node) {
                	if(node.data.netCode) {
	                	$("#paramBox").show();
	                	$("#stroragePath").hide();
	                	$("#stroragePath option").remove();
	                	$("#stroragePathLabel").hide();
	                	$("#createStatisticFileBut").hide();
	                	$("#updateStatisticFileBox").hide();
	                	$("#waitLabel").hide();
	                	$("#downloadFile").hide();
	                	$("#recreateStatisticFileBut").hide();
	                	$("#dealerLabel").text("您选择了经销商：" + node.data.text);
	                	var dc = node.data.netCode;
	                	$("#dc").val(dc);
	                	prepareSPath(dc);
                	}
                },
                data: getDealers()
            });
            tree.collapseAll();
        });
        
        function prepareSPath(dc) {
        	$.post("<s:url value='/record/getStroragePaths.action'/>",{dealerCode : dc}, function(data) {
        		var datas = data.split(":");
        		if(datas[0] == 0) {
        			$("#stroragePath").show();
        			$("#stroragePathLabel").show();
        			$("#stroragePath").prepend("<option value='0'>请选择...</option>");
        			var dirs = datas[1].split(",");
        			for(var i = 0; i < dirs.length; i++) {
        				var dir = dirs[i].replace(/\n/g, "").replace(/\r/g, "");
        				$("#stroragePath").append("<option value='" + dir + "'>" + dir + "</option>");
        			}
        		} else {
        			$.ligerDialog.alert('该经销商尚未上传数据!', '提示', 'none'); 
        		}
        	});
        }
        
        function handleChange() {
        	var dc = $("#dc").val();
			var val = $("#stroragePath").val();
			if(val != 0) {
				$.post("<s:url value='/record/statisticsFileExist.action'/>",{dealerCode:dc, timeStamp:val}, function(r) {
					if(r == 0) {
						$("#createStatisticFileBut").hide();
   						$("#updateStatisticFileBox").show();
   						$("#downloadFile").attr("href","<s:url value='/execl-temp/'/>" + dc + "-" + val + ".xls");
   	        			$("#downloadFile").text("上海大众库存信息整理表" + dc + "-" + val + ".xls");
   	        			$("#downloadFile").show();
   	        			$("#recreateStatisticFileBut").show();
					} else {
						$("#createStatisticFileBut").show();
						$("#updateStatisticFileBox").hide();
						$("#downloadFile").hide();
						$("#recreateStatisticFileBut").hide();
					}
				});
				
				$("#tabs").show();
				$("#ts").val(val);
				var tabs = $("#tabs").ligerTab();
				$("#index0").attr("src", "<s:url value='/record/toSatusList.action?index=0'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
				$("#index1").attr("src", "<s:url value='/record/toSatusList.action?index=1'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
				$("#index2").attr("src", "<s:url value='/record/toSatusList.action?index=2'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
				$("#index3").attr("src", "<s:url value='/record/toSatusList.action?index=3'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
				$("#index4").attr("src", "<s:url value='/record/toSatusList.action?index=4'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
				$("#index5").attr("src", "<s:url value='/record/toSatusList.action?index=5'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
				$("#index6").attr("src", "<s:url value='/record/toSatusList.action?index=6'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
			}
        }
        
        function createStatisticFile() {
        	$.ligerDialog.confirm('请确认所有数据都已上传', function (yes) {
        		if(yes) {
		        	var dc = $("#dc").val();
		        	var ts = $("#ts").val();
		        	$("#createStatisticFileBut").attr("disabled","disabled");
		        	$("#waitLabel").show();
		        	$.post("<s:url value='/record/createStatistic.action'/>",{dealerCode:dc,timeStamp:ts}, function(data) {
		        		if(data == 0) {
		        			$("#createStatisticFileBut").hide();
		        			$("#waitLabel").hide();
		        			$("#downloadFile").attr("href","<s:url value='/execl-temp/'/>" + dc + "-" + ts + ".xls");
		        			$("#downloadFile").text("上海大众库存信息整理表" + dc + "-" + ts + ".xls");
		        			$("#downloadFile").show();
		        			$("#recreateStatisticFileBut").show();
		        			$("#updateStatisticFileBox").show();
		        		} else {
		        			$.ligerDialog.alert('后台忙，生成统计数据失败，请稍候再试!', '提示', 'warn');
		        			$("#createStatisticFileBut").attr("disabled","");
		        		}
		        	});
	        	}
        	});
        }
        
        function recreate() {
        	$.ligerDialog.confirm('确定要重新生成统计表格?', function (yes) {
        		if(yes) {
		        	var dc = $("#dc").val();
		        	var ts = $("#ts").val();
		        	$("#downloadFile").hide();
				 	$("#waitLabel").show();
				 	$("#recreateStatisticFileBut").attr("disabled","disabled");
				 	$("#textfield").attr("disabled","disabled");
				 	$("#filebtn").attr("disabled","disabled");
				 	$("#ufilebtn").attr("disabled","disabled");
				 	$.post("<s:url value='/record/createStatistic.action'/>",{dealerCode:dc,timeStamp:ts}, function(data) {
				 		if(data == 0) {
				 			$("#waitLabel").hide();
				 			$("#downloadFile").show();
				 			$("#recreateStatisticFileBut").attr("disabled","");
				 			$("#textfield").attr("disabled","");
						 	$("#filebtn").attr("disabled","");
						 	$("#ufilebtn").attr("disabled","");
				 		} else {
				 			$.ligerDialog.alert('后台忙，生成统计数据失败，请稍候再试!', '提示', 'warn');
				 			$("#waitLabel").hide();
				 			$("#downloadFile").show();
				 			$("#recreateStatisticFileBut").attr("disabled","");
				 			$("#textfield").attr("disabled","");
						 	$("#filebtn").attr("disabled","");
						 	$("#ufilebtn").attr("disabled","");
				 		}
				 	});
	        	}
        	});
        }
        
        function updateStatisticFile() {
        	var fileName = $("#statisticFile").val();
        	var errorMsgTitle = "错误";
    		if(fileName == "") {
    			$.ligerDialog.alert("请选择统计文件", errorMsgTitle, "error");
    			return false;
    		}
    		
    		$.ligerDialog.confirm('请确定确定统计文件格式是否正确', function (yes) { 
    			var suffix = fileName.substr(fileName.lastIndexOf('.') + 1).toLowerCase();
    			if(suffix != "xls") {
    				$.ligerDialog.alert("统计文件格式不正确，请上传excel文件", errorMsgTitle, "error");
    				return false;
    			}
    			
    			$("#waitLabel").show();
    			$("#downloadFile").hide();
    			$("#recreateStatisticFileBut").hide();
    			$("#updateStatisticFileBox").attr("disabled","disabled");
    			
   			 	$.ajaxFileUpload ({
 		                url:"<s:url value='/record/updateStatisticFile.action'/>",
 		                secureuri:false,
 		                fileElementId:'statisticFile',
 		                dataType: 'text',
 		                success: function (data, status) {
 		                   if(data.indexOf("1</pre>") != -1) {
 		                	  $.ligerDialog.alert("统计文件不正确，请上传正确文件", errorMsgTitle, "error");
 		                   } else if(data.indexOf("0</pre>") != -1) {
 		                	  $.ligerDialog.alert("更新成功", "更新成功", "success");
 		                   } else {
 		                	  $.ligerDialog.alert("系统忙,请稍后再试!", "提示", "warn");
 		                   }
 		                   $("#waitLabel").hide();
 		                   $("#downloadFile").show();
 		                  $("#recreateStatisticFileBut").show();
 		                  $("#updateStatisticFileBox").attr("disabled","");
 		                },
 		                error: function (data, status, e) {
 		                   alert(e);
 		                   $("#waitLabel").hide();
 		                   $("#downloadFile").show();
 		                   $("#recreateStatisticFileBut").show();
 		                  $("#updateStatisticFileBox").attr("disabled","");
 		                }
   		       	});
    			return true;
    		});
    		return false;
        }
    </script>
    <style type="text/css">
		.file-box {position:relative; width:510px; padding-top: 2px;}
    	.txt {height:20px; border:1px solid #cdcdcd; width:180px;} 
		.file {position:absolute; right:205px; top:0; height:24px; filter:alpha(opacity:0); opacity:0;}
    	.btn {background-color:#FFF; border:1px solid #CDCDCD; height:24px; width:120px;}
    </style>
</head>
<body style="padding:10px">
	<input id="dc" name="dc" type="hidden"/>
	<input id="ts" name="ts" type="hidden"/>
	<div style="width:1680px;">
	<div style="width: 300px; height: 800px; margin-left: 5px; margin-right: 1px; float:left; border: 1px solid #A3C0E8;">
		<h4>请先选择经销商</h4>
		<ul id="tree1"></ul>
	</div>

	<div style="width:80%; height:800px;border:1px solid #A3C0E8; float: right">
		<div id="paramBox" style="margin: 5px;">
			<label id="dealerLabel"></label>
			<br/>
			<label id="stroragePathLabel">请选择原始数据上传日期：</label><select id="stroragePath" onchange="handleChange()"></select>
			<s:if test="#session.role.name=='admin'">
				<br/>
				<input id="createStatisticFileBut" type="button" value="生成统计表格" onclick="createStatisticFile()" class="btn"/>
				<div class="file-box" id="updateStatisticFileBox"> 
					<form id="uploadFileForm" action="#">
						<input type='text' id='textfield' name='textfield' class='txt' /> 
						<input type='button' id='filebtn' class='btn' value='选择统计文件...' /> 
						<input type="file" name="statisticFile" size="28" class="file" id="statisticFile" onchange="document.getElementById('textfield').value=this.value" /> 
						<input type="button" id='ufilebtn' class="btn" value="上传完整信息整理表" onclick="updateStatisticFile()"/> 
					</form> 
				</div> 
				<br/>
				<input id="recreateStatisticFileBut" type="button" value="重新生成统计表格" onclick="recreate()" class="btn"/>
				<label id="waitLabel">正在生成或更新统计数据,请稍候...</label>
				<a id="downloadFile" href="#" target="_blank" title="请点击鼠标右键，选择链接另存为"></a>
			</s:if>
		</div>
		
		<div id="tabs" style="width:100%;">
			<div  title="在库车辆-状态一"  style="height:780px" >
				<iframe frameborder="0" id="index0" name="index0" src=""></iframe>
			</div>
			<div title="在库车辆-状态二"   style="height:780px" >
				<iframe frameborder="0" id="index1" name="index1" src=""></iframe>
			</div>
			<div title="在库车辆-状态三"   style="height:780px" >
				<iframe frameborder="0" id="index2" name="index2" src=""></iframe>
			</div>
			<div  title="系统外车辆"  style="height:780px">
				<iframe frameborder="0" id="index3" name="index3" src=""></iframe>
			</div>
			<div  title="在途车辆"   style="height:780px">
				<iframe frameborder="0" id="index4" name="index4" src=""></iframe>
			</div>
			<div  title="未提车辆"  style="height:780px">
				<iframe frameborder="0" id="index5" name="index5" src=""></iframe>
			</div>
			<div  title="虚拟未查到" style="height:780px">
				<iframe frameborder="0" id="index6" name="index6" src=""></iframe>
			</div>
		</div>
	</div>
	</div>
</body>
</html>